package rmmk.algorithms.preprocessing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import rmmk.algorithms.preprocessing.filters.RemoveMostCommonFilter;
import rmmk.framework.datasources.Document;

/**
 * Klasa zliczająca globalnie ilość wystąpień danego słowa dla wszystkich
 * dokumentów
 * 
 * @author marcin
 * 
 */
public class GlobalWordCounts {

	private static Logger logger = LoggerFactory
			.getLogger(GlobalWordCounts.class);

	private static HashMap<String, Integer> results = new HashMap<>();

	public static void process(List<Document> documents) {

		// int cores = Runtime.getRuntime().availableProcessors();

		// temp = Collections.synchronizedMap(results);

		logger.info("Global words count started");
		for (Document doc : documents)
			for (String word : doc.getExtractedWords()) {
				if (results.containsKey(word)) {
					int i = results.get(word);
					results.put(word, ++i);
				} else
					results.put(word, 1);
			}

		logger.info("Global words count ended with " + results.size()
				+ " words");
	}

	public static HashMap<String, Integer> getWordCount() {
		return results;
	}

	private static String[] mostCommonCache = null;

	public static String[] getGlobalWords() {
		if (mostCommonCache != null)
			return mostCommonCache;

		HashMap<Integer, String> reverse = new HashMap<Integer, String>();

		for (Entry<String, Integer> e : results.entrySet())
			reverse.put(e.getValue(), e.getKey());

		List<String> mostCommon = new ArrayList<>();

		int howMany = results.size() - (results.size() / 4);
		int i = 0;

		for (Entry<Integer, String> result : reverse.entrySet())
			if (!(i++ < howMany))
				mostCommon.add(result.getValue());
		
		for(String s : RemoveMostCommonFilter.common)
			mostCommon.add(s);

		return mostCommonCache = mostCommon.toArray(new String[mostCommon.size()]);
	}
}
